import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
from matplotlib.axes._base import _AxesBase
from matplotlib.text import Text
from pandas import DataFrame
from pandas.core.groupby import SeriesGroupBy

trn_click = pd.read_csv("../data/train_click_log.csv")
item_df = pd.read_csv("../data/articles.csv")
item_df = item_df.rename(columns={"article_id": "click_article_id"})
# item_emb_df = pd.read_csv("../data/articles_emb.csv")
tst_click = pd.read_csv("../data/testA_click_log.csv")

click_timestamp: SeriesGroupBy = trn_click.groupby(["user_id"])["click_timestamp"]
click_timestamp: DataFrame = click_timestamp.rank(ascending=False)
click_timestamp: DataFrame = click_timestamp.astype(int)
trn_click["rank"] = click_timestamp
click_timestamp: SeriesGroupBy = tst_click.groupby(["user_id"])["click_timestamp"]
click_timestamp: DataFrame = click_timestamp.rank(ascending=False)
click_timestamp: DataFrame = click_timestamp.astype(int)
tst_click["rank"] = click_timestamp

click_timestamp: SeriesGroupBy = trn_click.groupby(["user_id"])["click_timestamp"]
click_timestamp: DataFrame = click_timestamp.transform("count")
trn_click["click_cnts"] = click_timestamp
click_timestamp: SeriesGroupBy = tst_click.groupby(["user_id"])["click_timestamp"]
click_timestamp: DataFrame = click_timestamp.transform("count")
tst_click["click_cnts"] = click_timestamp

pd.set_option("display.max_columns", None)
# pd.set_option("display.max_rows", None)

trn_click = trn_click.merge(item_df, how="left", on="click_article_id")


# print(trn_click.head())

# print(trn_click.info())

# print(trn_click.describe())

# print(trn_click["user_id"].nunique())

# clickArticleGroup: SeriesGroupBy = trn_click.groupby(["user_id"])["click_article_id"]
# print(clickArticleGroup.count().min())

# plt.figure()
# plt.figure(figsize=(15, 20))
# i = 1
# for col in ['click_article_id', 'click_timestamp', 'click_environment', 'click_deviceGroup', 'click_os',
#             'click_country',
#             'click_region', 'click_referrer_type', 'rank', 'click_cnts']:
#     plot_envs = plt.subplot(5, 2, i)
#     i += 1
#     v = trn_click[col].value_counts().reset_index()[:10]
#     fig: _AxesBase = sns.barplot(x=v["index"], y=v[col])
#     for item in fig.get_xticklabels():
#         item: Text = item
#         item.set_rotation(90)
#     plt.title(col)
# plt.tight_layout()
# plt.show()

def plot_envs(df: DataFrame, cols, r, c):
    """
    画图，分析每个特征的分布
    """
    plt.figure()
    plt.figure(figsize=(10, 5))
    i = 1
    for col in cols:
        plt.subplot(r, c, i)
        i += 1
        v = df[col].value_counts().reset_index()
        fig: _AxesBase = sns.barplot(x=v["index"], y=v[col])
        for item in fig.get_xticklabels():
            item: Text = item
            item.set_rotation(90)
        plt.title(col)
    plt.tight_layout()
    plt.show()


# print(trn_click["click_environment"].value_counts())

# print(trn_click["click_deviceGroup"].value_counts())

tst_click = tst_click.merge(item_df, how="left", on="click_article_id")

# print(tst_click.head())

# print(tst_click.describe())

# print(tst_click["user_id"].nunique())

# tst_clickArticleGroup: SeriesGroupBy = tst_click.groupby(["user_id"])["click_article_id"]
# print(tst_clickArticleGroup.count().min())

# print(item_df.head().append(item_df.tail()))

# print(item_df["words_count"].value_counts())

# print(item_df["category_id"].nunique())
# item_df["category_id"].hist()
# plt.show()

# print(item_df.shape)

# print(item_emb_df.head())
# print(item_emb_df.shape)

user_click_merge = trn_click.append(tst_click)

# user_click_group: SeriesGroupBy = user_click_merge.groupby(["user_id", "click_article_id"])["click_timestamp"]
# user_click_count: DataFrame = user_click_group.agg({"count"})
# user_click_count = user_click_count.reset_index()
#
# print(user_click_count[user_click_count["count"] > 7])
#
# print(user_click_count["count"].unique())
# print(user_click_count["count"].value_counts())

# sample_user_ids = np.random.choice(user_click_merge["user_id"], size=10, replace=False)
# sample_users = user_click_merge[user_click_merge["user_id"].isin(sample_user_ids)]
# cols = ['click_environment', 'click_deviceGroup', 'click_os', 'click_country', 'click_region', 'click_referrer_type']
# for _, user_df in sample_users.groupby(["user_id"]):
#     plot_envs(user_df, cols, 2, 3)

user_click_item_count = sorted(user_click_merge.groupby("user_id")["click_article_id"].count(), reverse=True)
# plt.plot(user_click_item_count)
# plt.plot(user_click_item_count[:50])
# plt.plot(user_click_item_count[25000:50000])
# plt.show()

item_click_count = sorted(user_click_merge.groupby("click_article_id")["user_id"].count(), reverse=True)

# plt.plot(item_click_count)
# plt.plot(item_click_count[:100])
# plt.plot(item_click_count[:20])
# plt.plot(item_click_count[3500:])
# plt.show()

# tmp = user_click_merge.sort_values("click_timestamp")
# tmp["next_item"] = tmp.groupby("user_id")["click_article_id"].transform(lambda x: x.shift(-1))
# union_item: DataFrame = tmp.groupby(["click_article_id", "next_item"])["click_timestamp"].agg({"count"}).reset_index() \
#     .sort_values("count", ascending=False)
# print(union_item.describe())

# x = union_item["click_article_id"]
# y = union_item["count"]
# plt.scatter(x, y)
# plt.show()

# plt.plot(union_item["count"].values[40000:])
# plt.show()

# plt.plot(user_click_merge["category_id"].value_counts().values)
# plt.show()
# plt.plot(user_click_merge["category_id"].value_counts().values[150:])
# plt.show()

# print(user_click_merge["words_count"].describe())

# plt.plot(user_click_merge["words_count"].values)
# plt.show()

# plt.plot(sorted(user_click_merge.groupby("user_id")["category_id"].nunique(), reverse=True))
# plt.show()

# print(user_click_merge.groupby("user_id")["category_id"].nunique().reset_index().describe())

# plt.plot(sorted(user_click_merge.groupby("user_id")["words_count"].mean(), reverse=True))
# plt.show()

# plt.plot(sorted(user_click_merge.groupby("user_id")["words_count"].mean(), reverse=True)[1000:45000])
# plt.show()

# print(user_click_merge.groupby("user_id")["words_count"].mean().reset_index().describe())

from sklearn.preprocessing import MinMaxScaler

mm = MinMaxScaler()
user_click_merge["click_timestamp"] = mm.fit_transform(user_click_merge[["click_timestamp"]])
user_click_merge["created_at_ts"] = mm.fit_transform(user_click_merge[["created_at_ts"]])

# user_click_merge = user_click_merge.sort_values("click_timestamp")
# print(user_click_merge.head())

# def mean_diff_time_func(df: DataFrame, col: str):
#     df = pd.DataFrame(df, columns={col})
#     df["time_shift1"] = df[col].shift(1).fillna(0)
#     df["diff_time"] = abs(df["time_shift1"] - df[col])
#     return df["diff_time"].mean()
#
#
# mean_diff_click_time = user_click_merge.groupby("user_id")[["click_timestamp", "created_at_ts"]].apply(
#     lambda x: mean_diff_time_func(x, "click_timestamp"))
# plt.plot(sorted(mean_diff_click_time.values, reverse=True))
# plt.show()
#
# mean_diff_created_time = user_click_merge.groupby("user_id")[["click_timestamp", "created_at_ts"]].apply(
#     lambda x: mean_diff_time_func(x, "created_at_ts"))
# plt.plot(sorted(mean_diff_created_time.values, reverse=True))
# plt.show()

# from gensim.models import Word2Vec
# import logging, pickle
#
#
# def trian_item_word2vec(click_df: DataFrame, embed_size=16, save_name='item_w2v_emb.pkl', split_char=' ') -> dict:
#     click_df = click_df.sort_values("click_timestamp")
#     click_df["click_article_id"] = click_df["click_article_id"].astype(str)
#     docs = click_df.groupby("user_id")["click_article_id"].apply(lambda x: list(x)).reset_index()
#     docs = docs["click_article_id"].values.tolist()
#     logging.basicConfig(format="%(asctime)s:%(levelname)s:%(message)s", level=logging.INFO)
#     w2v = Word2Vec(docs, size=16, sg=1, window=5, seed=2020, workers=24, min_count=1, iter=10)
#     item_w2v_emb_dict = {k: w2v.wv[k] for k in click_df["click_article_id"]}
#     return item_w2v_emb_dict
#
#
# item_w2v_emb_dict = trian_item_word2vec(user_click_merge)
#
# sub_user_ids = np.random.choice(user_click_merge["user_id"].unique(), size=15, replace=False)
# sub_user_info = user_click_merge[user_click_merge["user_id"].isin(sub_user_ids)]
# print(sub_user_info.head())
#
#
# def get_item_sim_list(df: DataFrame):
#     sim_list = []
#     item_list = df["click_article_id"].values
#     for i in range(0, len(item_list) - 1):
#         emb1 = item_w2v_emb_dict[str(item_list[i])]
#         emb2 = item_w2v_emb_dict[str(item_list[i + 1])]
#         sim_list.append(np.dot(emb1, emb2) / (np.linalg.norm(emb1) * np.linalg.norm(emb2)))
#     sim_list.append(0)
#     return sim_list
#
#
# for _, user_df in sub_user_info.groupby("user_id"):
#     item_sim_list = get_item_sim_list(user_df)
#     plt.plot(item_sim_list)
# plt.show()
